﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Linq;
using System.Windows;
using System.Collections.ObjectModel;


namespace QuanLyDuocPham.Controllers.QuanLyHeThong.DanhMuc
{
    class KHO_Control: IDisposable
    {
        DuocPhamDataContext db = new DuocPhamDataContext(staticClass.Conn);
        public void Dispose()
        {
            if (db != null)
            {
                db.Dispose();
                db = null;
            }
        }
        public void loadComboBox(ComboBox cmb)
        {

            cmb.Items.Clear();
            var list = db.KHO_Get_List();
            var typeorder = list.OrderBy(w => w.TEN_KHO);
            foreach (var t in typeorder)
            {
                cmb.Items.Add(t);
            }
            cmb.SelectedValuePath = "MA_KHO";
            cmb.IsTextSearchEnabled = true;
            cmb.IsTextSearchCaseSensitive = false;
            cmb.IsEditable = true;
            KHO_Get_ListResult temp = new KHO_Get_ListResult();

            temp.MA_KHO = "...";
            temp.TEN_KHO = "Thêm mới";
            cmb.Items.Add(temp);
        }

        public string loadID_moi()
        {
            string ID = "";

            int rowcount = db.KHO_Get_List().Count();
            if (rowcount == 0)
            {
                ID = "KHO0001";
            }
            else
            {
                for (int i = 1; i < rowcount+2; i++)
                {
                    string IDtam = "KHO";
                    for (int j = 0; j < (4 - i.ToString().Length); j++)
                        IDtam = IDtam + "0";
                    IDtam = IDtam + i;
                    Boolean kq = true;
                    var data = db.KHO_Get_List();

                    foreach (var t in data)
                    {

                        if (t.MA_KHO.Trim() == IDtam.Trim())

                            kq = false;
                    }
                    if (kq == true)
                    {
                        ID = IDtam;
                        break;
                    }
                }

            }
            return ID;

        }


        public void addData(string ma_kho, TextBox txtTenKho, TextBox txtDia_Chi, TextBox txtD_Thoai, TextBox txtFax, RichTextBox txtGhi_Chu)
        {
            if (validateData(txtTenKho, txtDia_Chi, txtD_Thoai))
                MessageBox.Show("Chưa nhập tên hoạt chất!");
            else
            {
                db.KHO_Insert(ma_kho, txtTenKho.Text, txtDia_Chi.Text, txtD_Thoai.Text, txtFax.Text, txtGhi_Chu.Document.Blocks.ToString());
                MessageBox.Show("Đã thêm thành công");
            }
        }

        private bool validateData(TextBox txtTenKho, TextBox txtDia_Chi, TextBox txtD_Thoai)
        {
            if (txtTenKho.Text.Trim() == "" || txtDia_Chi.Text.Trim() == "" || txtD_Thoai.Text.Trim() == "" || txtD_Thoai.Text.Trim() == "")
            {
                return false;
            }
            return true;
        }
        public void loadData(ListView List)
        {
            List.Items.Clear();
            var list = db.KHO_Get_List();
            var typeorder = list.OrderBy(w => w.TEN_KHO);
            foreach (var t in typeorder)
            {
                List.Items.Add(t);
            }
        }
        public void loadData(DataPager dp)
        {
            
            var list = db.KHO_Get_List();
            var typeorder = list.OrderBy(w => w.TEN_KHO);
            ObservableCollection<Object> dpSource = new ObservableCollection<Object>();
            int i = 1;
            foreach (var t in typeorder)
            {
                if (t.TEN_KHO.Trim() != "")
                {
                    dpSource.Add(new { STT = i, t.TEN_KHO, t.MA_KHO, t.GHI_CHU, t.FAX, t.DIEN_THOAI, t.DIA_CHI });
                    i++;
                }
            }
            dp.ItemsSource = dpSource;
            dp.ItemsPerPage = 10;
        }
        public void updateData(string ma_kho, TextBox txtTenKho, TextBox txtDia_Chi, TextBox txtD_Thoai, TextBox txtFax, RichTextBox txtGhi_Chu)
        {
            if (validateData(txtTenKho, txtDia_Chi, txtD_Thoai))
                MessageBox.Show("Chưa nhập tên hoạt chất!");
            else
            {
                db.KHO_Update(ma_kho, ma_kho, txtTenKho.Text, txtDia_Chi.Text, txtD_Thoai.Text, txtFax.Text, txtGhi_Chu.Document.Blocks.ToString());
                MessageBox.Show("Đã cập nhật!");
            }
        }

        public string deleteData(string ID_Delete, string error)
        {
            error = "";
            if (db.THUOC_KHO_GetByMaKho(ID_Delete).Count() > 0 || db.PHIEUCHUYENKHO_GetByMaKho(ID_Delete).Count() > 0 || db.PHIEUXUAT_GetByMaKho(ID_Delete).Count() > 0 || db.NHANVIEN_GetByMaKho(ID_Delete).Count() > 0)
                error = "Không thể xóa";
            else
            {
                db.DONTHUOC_Delete(ID_Delete);
                error = "Xóa thành công!";
            }
            return error;


        }
        public void loadForm_Edit(string ID_Edit,  TextBox txtTenKho, TextBox txtDia_Chi, TextBox txtD_Thoai, TextBox txtFax, RichTextBox txtGhi_Chu)
        {

            var sl = (KHO_GetByMaKhoResult)db.KHO_GetByMaKho(ID_Edit).Single();


            
            txtTenKho.Text = sl.TEN_KHO;
            txtDia_Chi.Text = sl.DIA_CHI;
            txtD_Thoai.Text = sl.DIEN_THOAI;
            txtFax.Text = sl.FAX;
               
            txtGhi_Chu.AppendText( sl.GHI_CHU);
        }
    }
}
